package in.smartride.client;

import java.util.List;

import in.smartride.dataInterface.RideOffer;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DateBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SmartRide implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public static String user;
	static int height = Window.getClientHeight();
	static int width = Window.getClientWidth();
	public static int landmarksretrieved = 0;
	static String landMarks[];
	private static DataRetriever dr = new DataRetriever();
	static DockPanel dock;
	static List<UserProfile> UPs;
	static int counter;

	public void onModuleLoad() {
		dr.getLandMarks();
	}

	public static void process() {
		addBar();
		DecoratorPanel findRide = createPanel("Find");
		DecoratorPanel offerRide = createPanel("Offer");
		DockPanel dock = new DockPanel();
		dock.add(findRide, DockPanel.EAST);
		dock.add(offerRide, DockPanel.WEST);
		dock.setSpacing(10);

		RootPanel.get().add(dock, width / 2 - width / 5,
				height / 2 - height / 5);
	}

	static void addBar() {
		MenuBar menuBar = new MenuBar();
		menuBar.setAnimationEnabled(true);
		menuBar.setAutoOpen(true);
		Command menuCommand = new Command() {
			private int curPhrase = 0;
			private final String[] phrases = { "to be implemented" };

			public void execute() {
				Window.alert(phrases[curPhrase]);
				curPhrase = (curPhrase + 1) % phrases.length;
			}
		};

		Command profileEdit = new EditProfileCommand();
		Command navigateHome = new Command() {

			@Override
			public void execute() {
				RootPanel.get().clear();
				process();
			}
		};
		Command editPreferences = new EditPreferences();
		menuBar.addItem(new MenuItem("Home", navigateHome));
		menuBar.addSeparator();
		MenuBar profile = new MenuBar(true);
		profile.setAnimationEnabled(true);
		profile.addItem(" view   ", menuCommand);
		profile.addItem(" edit   ", profileEdit);
		menuBar.addItem(new MenuItem("Profile", profile));
		menuBar.addSeparator();
		menuBar.addItem(new MenuItem("Preferences", editPreferences));
		menuBar.addSeparator();
		menuBar.addItem(new MenuItem("MapBuilder", new ShowMapBuilder()));
		menuBar.addSeparator();
		menuBar.addItem(new MenuItem("Logout", menuCommand));
		int hPosition = width * 5 / 8;
		int vPosition = height * 4 / 17;
		RootPanel.get().add(menuBar, hPosition - 40, vPosition);

	}

	private static DecoratorPanel createPanel(final String str) {
		DecoratorPanel panel = new DecoratorPanel();
		panel.setPixelSize(width / 5, height / 5);
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		layout.setTitle(str + " Ride");
		layout.setHTML(0, 0, str + " Ride");
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();

		for (int i = 0; i < landMarks.length; ++i) {
			oracle.add(landMarks[i].split("@")[0]);
		}

		// Create the from suggest box
		final SuggestBox fromSuggestBox = new SuggestBox(oracle);
		// fromSuggestBox.ensureDebugId("cwSuggestBox");

		// Create the to suggest box
		final SuggestBox toSuggestBox = new SuggestBox(oracle);
		fromSuggestBox.ensureDebugId("cwSuggestBox");

		// Add some standard form options
		layout.setHTML(1, 0, "from");
		layout.setWidget(1, 1, fromSuggestBox);
		layout.setHTML(2, 0, "to");
		layout.setWidget(2, 1, toSuggestBox);
		layout.setHTML(3, 0, "date");
		final DateBox dateBox = new DateBox();
		layout.setWidget(3, 1, dateBox);

		// add button
		Button button = new Button(str, new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(fromSuggestBox.getText() != null && toSuggestBox.getText() != null &&
						dateBox.getValue() != null)
					dr.processRequest(str, fromSuggestBox.getText(), toSuggestBox.getText(),
							dateBox.getValue());
			}
		});
		button.ensureDebugId("cwBasicButton-normal");
		layout.setWidget(4, 1, button);

		panel.setWidget(layout);
		return panel;
	}

	public static void showavailableRideOffers(String rideOffers) {

		System.out.println("In function show ride offers");
		if (rideOffers.equals("-1")) {
			Window
					.alert("No match Found!! You will be intimated as soon as we find a smartRider!");
		} else {
			RootPanel.get().clear();
			addBar();
			System.out.println(rideOffers);
			String RO[] = rideOffers.split("-");
			for(int i=0;i<RO.length;i++){
				String ride[] = RO[i].split(",");
				DisclosurePanel rideOffer = new DisclosurePanel(
						ride[4] + " " + ride[5]);
				rideOffer.setContent(new HTML("<b>Ride Information</b>: <br/>" +
						"<b>From</b>: " + ride[1] + "<br/>" +
						"<b>To</b>: " + ride[2] + "<br/>" +
						"<b>Date</b>: " + ride[3] + "<br/>" +
						"<br/> <b>User Information</b>: <br/>" +
						"<b>Date of Birth</b>: " + ride[7] + "<br/>" +
						"<b>Gender</b>: " + (Boolean.parseBoolean(ride[6])==true?"Male":"Female") + "<br/>" + 
						"<b>Mobile Number</b>: " + ride[8] + "<br/>"
						));
				rideOffer.setAnimationEnabled(true);
				RootPanel.get().add(rideOffer);
			}
		}
	}
}
